package com.secneo.appshare.activities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.common.ActivityUtil;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.common.Update;
import com.secneo.mp.MpApi;

public class HelpWebViewActivity2 extends Activity{

	ListView list = null;
	public static String URL;
	ArrayList<HashMap<String,String>> data = null;
	
	final Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sec_opt_help_webview2);
		AppShareApplication.getInstance().addActivity(this);
		initViews();
	}

	private void initViews()
	{
		list = (ListView) findViewById(R.id.list);
		ListAdapter adapter = new SimpleAdapter(this, getData(), R.layout.sec_opt_scan_list_item5, new String[]{"title"}, new int[]{R.id.helpListText});
		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				String title = data.get(position).get("title");
				if (title.equals(getString(R.string.help_question)))
				{
					HelpWebViewActivity.go(HelpWebViewActivity2.this, URL, getString(R.string.help_question));
				}
				else if (title.equals(getString(R.string.help_update)))
				{
					if(ActivityUtil.networkAvailable(HelpWebViewActivity2.this))
					{
					MpApi mp = MpApi.getAPI();
					SharedPreferences idSaving = getSharedPreferences("first_config", 0);
					Update.checkVersion(HelpWebViewActivity2.this,
							HelpWebViewActivity2.this, handler,
								Constants.PACKAGENAME, String.valueOf(Constants.VERSION_CODE), "1", 1, mp.getPhoneMd5Imei(HelpWebViewActivity2.this), idSaving.getString("channel_id", ""));
				
					}
				}
				else if (title.equals(getString(R.string.help_feedback)))
				{
					Intent i = new Intent();
					i.setClass(HelpWebViewActivity2.this,FeedBackActivity.class);
					startActivity(i);
				}
				else if (title.equals(getString(R.string.help_about)))
				{
					Intent i = new Intent();
					i.setClass(HelpWebViewActivity2.this,AboutActivity.class);
					startActivity(i);
				}
			}
		});
		
		Button mReturnBtn = (Button) findViewById(R.id.returnBtn);
		OnClickListener mReturnListener = new OnClickListener() 
		{
			public void onClick(View v) 
			{
				finish();
			}
		};
		mReturnBtn.setOnClickListener(mReturnListener);
	}

	private List<? extends Map<String, ?>> getData()
	{
		data = new ArrayList<HashMap<String,String>>();
		data.add(getTitleMap(getString(R.string.help_question)));
		data.add(getTitleMap(getString(R.string.help_update)));
		data.add(getTitleMap(getString(R.string.help_feedback)));
		data.add(getTitleMap(getString(R.string.help_about)));
		return data;
	}

	private HashMap<String,String> getTitleMap(String string)
	{
		HashMap<String,String> map = new HashMap<String,String>();
		map.put("title", string);
		return map;
	}
	
	
	public static void go(Context context,String url,String title)
	{
		URL = url;
		Intent i = new Intent(context, HelpWebViewActivity2.class);
		context.startActivity(i);
	}
}
